package com.ysd.lis.util;


import cn.hutool.json.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * parentId为当前List最上层父id
 * idKey为实体类对象中id键名
 * parentKey为实体类对象中id的键名
 * childName为返回数据子列表的命名
 */
@Slf4j
public class ListToTreeUtil {
    public static JSONArray forObjectToTreeMap(List<?> treeList, Long parentId, String idKey, String parentIdKey, String childName) {
        JSONArray childMenu = new JSONArray();
        for (Object object : treeList) {
            //JSONObject jsonMenu = JSONUtil.parseObj(object);
            JSONObject jsonMenu = JSONObject.parseObject(JSONObject.toJSONString(object));

            Long menuId = jsonMenu.getLong(idKey);
            Long pid = jsonMenu.getLong(parentIdKey);
            if (parentId.equals(pid) ) {
                JSONArray array = forObjectToTreeMap(treeList, menuId,idKey,parentIdKey,childName);
                jsonMenu.put(childName, array);
                childMenu.add(jsonMenu);
            }
        }
        return childMenu;
    }

    public static JSONArray forObjectToTree(List<?> treeList, String parentId, String idKey, String parentIdKey, String childName) {
        JSONArray childMenu = new JSONArray();
        for (Object object : treeList) {
            //JSONObject jsonMenu = JSONUtil.parseObj(object);
            JSONObject jsonMenu = JSONObject.parseObject(JSONObject.toJSONString(object));

            String menuId = jsonMenu.getString(idKey);
            String pid = jsonMenu.getString(parentIdKey);
            if (parentId.equals(pid) ) {
                JSONArray array = forObjectToTree(treeList, menuId,idKey,parentIdKey,childName);
                jsonMenu.put(childName, array);
                childMenu.add(jsonMenu);
            }
        }
        return childMenu;
    }

    public static JSONArray forObjectAndLevelToTree(List<?> treeList, String parentId, String idKey, String parentIdKey, String childName,Integer level) {
        JSONArray childMenu = new JSONArray();
        for (Object object : treeList) {
            //JSONObject jsonMenu = JSONUtil.parseObj(object);
            JSONObject jsonMenu = JSONObject.parseObject(JSONObject.toJSONString(object));

            String menuId = jsonMenu.getString(idKey);
            String pid = jsonMenu.getString(parentIdKey);
            jsonMenu.put("level",level);
            if (parentId.equals(pid) ) {
                JSONArray array = forObjectAndLevelToTree(treeList, menuId,idKey,parentIdKey,childName,level+1);
                jsonMenu.put(childName, array);
                childMenu.add(jsonMenu);
            }
        }
        return childMenu;
    }


    public static void main(String[] args) {
        List menuList = new ArrayList();

        Map map1 = new HashMap<>();
        map1.put("id", 1);
        map1.put("name", "食品");
        map1.put("pid", 0);

        Map map2 = new HashMap<>();
        map2.put("id", 2);
        map2.put("name", "饼干");
        map2.put("pid", 1);

        Map map3 = new HashMap<>();
        map3.put("id", 3);
        map3.put("name", "DJB");
        map3.put("pid", 1);

        menuList.add(map1);
        menuList.add(map2);
        menuList.add(map3);
//        JSONArray menuarray = ListToTreeUtil.forObjectToTreeMap(menuList, (long) 0, "id", "pid", "children");
//        System.out.println(menuarray);
    }

}
